package com.qinglei.recoup.longconn.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.qinglei.recoup.common.utils.JsonUtils;
import com.qinglei.recoup.longconn.entity.MessageV4_1;
import com.qinglei.recoup.longconn.enums.ProtocolVersionEnum;
import com.qinglei.recoup.longconn.service.IotMessageStrategyService;
import com.qinglei.recoup.longconn.service.RadarDataV4_1Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 物联网消息处理协议V4实现
 *
 * @author yujunwei
 * @datetime 2022-03-02 17:13
 */
@Service
@Slf4j
public class IotMessageStrategyV4ServiceImpl implements IotMessageStrategyService {

    @Autowired
    private RadarDataV4_1Service radarDataV4_1Service;

    @Override
    public String protocolVersion() {
        return ProtocolVersionEnum.V4.getVersion();
    }

    @Override
    public void handleMethod(Map<String, Object> map) {
        String sn = map.get("sn").toString();
        String content = map.get("content").toString();
        JsonNode itemsNode = JsonUtils.stringToJson(content).get("items");

        MessageV4_1 message = radarDataV4_1Service.parseHealthCareUserData(sn, itemsNode);
        radarDataV4_1Service.procUploadDataMessageV4_1(message);
        if (message != null) {
            log.info("into ProtocolVersion 4 handle -> sn:{}, frameNo:{}", message.getSn(), message.getBody().getFrameNo());
        }
    }
}
